const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,

        // 总条数
        total: 0,
        pageNum: 1,
        pageSize: 10,
        dataList: [],

        userId: "",
        approveStatus: "",
        searchValue: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserApplyList();
    },

    /**
     * 搜索内容相关字段
     *
     * @param e
     */
    searchInput: function (e) {
        let self = this;
        self.setData({
            searchValue: e.detail.value
        })
    },

    /**
     * 查询
     */
    searchButton: function () {
        this.setData({
            pageNum: 1,
            pageSize: 10,
            dataList: []
        });
        this.getUserApplyList();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            pageNum: 1,
            pageSize: 10,
            searchValue: "",
            dataList: []
        });
        this.getUserApplyList();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.pageSize == this.data.dataList.length) {
            this.setData({
                pageNum: this.data.pageNum + 1
            });
            this.getUserApplyList();
        } else {
            return;
        }
    },

    /**
     * 获取需要审核数据
     */
    getUserApplyList: function () {
        let self = this;
        let params = {
            pageNum: self.data.pageNum,
            pageSize: self.data.pageSize,
            searchValue: self.data.searchValue
        }

        app.fetchApis(self, qCloud.api.getUserApplyList, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    total: e.data.total
                })
                let dataList = e.data.rows;
                dataList = [...self.data.dataList, ...dataList]
                self.setData({
                    dataList: dataList
                })
            }
        })
    },

    /**
     * 审核通过
     * @param e
     */
    handlePass: function (e) {
        let self = this;
        const userId = e.currentTarget.dataset["index"];

        wx.showModal({
            title: "确定要审核通过用户申请吗？",
            success: function (check) {
                self.setData({
                    pageNum: 1,
                    pageSize: 10,
                    searchValue: "",
                    dataList: []
                });
                let params = {
                    userId: userId,
                    approveStatus: "1"
                };

                if (check.confirm) {
                    app.fetchApis(self, qCloud.api.auditUserApply, params, "POST", function (res) {
                        if (res.data.code == 200) {
                            self.getUserApplyList();
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: "error"
                            })
                        }
                    });
                } else {
                    self.getUserApplyList();
                }
            },
        })
    },

    /**
     * 审核驳回
     *
     * @param e
     */
    handleReject: function (e) {
        let self = this;
        const userId = e.currentTarget.dataset["index"];

        wx.showModal({
            title: "确定要驳回用户申请吗？",
            success: function (check) {
                self.setData({
                    pageNum: 1,
                    pageSize: 10,
                    searchValue: "",
                    dataList: []
                });
                let params = {
                    userId: userId,
                    approveStatus: "1"
                };

                if (check.confirm) {
                    app.fetchApis(self, qCloud.api.auditUserApply, params, "POST", function (res) {
                        if (res.data.code == 200) {
                            self.getUserApplyList();
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: "error"
                            })
                        }
                    });
                } else {
                    self.getUserApplyList();
                }
            },
        })
    }
})
